package edu.scut.course_management.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.result.JsonResultFactory;
import edu.scut.course_management.result.ResultCode;
import edu.scut.course_management.util.ResponseWritingUtil;

/**
 * 访问拒绝处理器
 * 用户访问权限不够的资源时的处理逻辑
 * 返回前端 json 数据，提示 “权限不足”
 */
@Component
public class NoPermissionHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        JsonResult result = JsonResultFactory.getInstance().buildFailResult(ResultCode.NO_PERMISSION);
        ResponseWritingUtil.writeJsonResultToResponse(response, result);
    }
}
